package ch.ethz.fcl.metrobuzz.data.interchange;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.newdawn.slick.util.ResourceLoader;

import ch.ethz.fcl.metrobuzz.data.MBData;
import ch.ethz.fcl.metrobuzz.data.singapore.SGInterchange;

/**
 * read junction node information from files
 * 
 * @author Zeng Wei
 * @email zeng@arch.ethz.ch
 * 
 */
public class JunctionReader {
	private MBData data;

	public JunctionReader(MBData data) {
		this.data = data;
	}

	public ArrayList<Junction> readJunctions(SGInterchange interchange,
			String fileName) {
		ArrayList<Junction> jns = new ArrayList<Junction>();

		try {
			InputStreamReader isr = new InputStreamReader(
					ResourceLoader.getResourceAsStream(fileName));
			BufferedReader br = new BufferedReader(isr);
			String s = null;
			while ((s = br.readLine()) != null) {
				String[] subs = s.split("\t");
				int index = Integer.parseInt(subs[1]);
				float x = Float.parseFloat(subs[3]);
				float y = Float.parseFloat(subs[5]);

				Junction jn = new Junction(index);
				jn.setCentroid(x, y);

				s = br.readLine(); // incoming edges
				subs = s.split("\t");
				for (int i = 1; i < subs.length; i++) {
					jn.addEdge(data.getScene().getEdge(subs[i]), true);
				}

				s = br.readLine(); // outgoing edges
				subs = s.split("\t");
				for (int i = 1; i < subs.length; i++) {
					jn.addEdge(data.getScene().getEdge(subs[i]), false);
				}

				s = br.readLine(); // nodes
				subs = s.split("\t");
				for (int i = 1; i < subs.length; i++) {
					if (subs[i] != null) {
						jn.addNode(data.getScene().getNode(subs[i]));
					}
				}

				s = br.readLine(); // color map
				subs = s.split("\t");

				for (int i = 1; i < subs.length; i++) {
					String[] ss = subs[i].split(":");
					int color = Integer.parseInt(ss[0]);
					jn.setColorMap(data.getScene().getEdge(ss[1]), color);
				}
				// only add MRT junctions to accelerate processing rate
				if (jn.isSubway())
					jns.add(jn);

				br.readLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return jns;
	}

	public void readJunctionDistribution(String fileName,
			SGInterchange interchange) {
		for (Junction jn : interchange.getJunctions())
			jn.init(data, interchange, data.getRoadNetwork());

		try {
			InputStreamReader isr = new InputStreamReader(
					ResourceLoader.getResourceAsStream(fileName));
			BufferedReader br = new BufferedReader(isr);
			String s = null;
			int index = 0;
			while ((s = br.readLine()) != null) {
				if (s.startsWith("index")) {
					String[] subs = s.split("\t");
					index = Integer.parseInt(subs[1]);
				} else {
					for (Junction jn : interchange.getJunctions()) {
						if (jn.getIndex() == index) {
							String[] subs = s.split("\t");
							String[] fromto = subs[0].split(":");
							int from = Integer.parseInt(fromto[0]);
							int to = Integer.parseInt(fromto[1]);
							for (int i = 1; i < subs.length; i++) {
								String[] timesize = subs[i].split(":");
								int time = Integer.parseInt(timesize[0]);
								int size = Integer.parseInt(timesize[1]);

								if (jn.distribution == null)
									jn.distribution = new int[jn.matrixSize][jn.matrixSize][MBData.ONE_DAY_TIME];

								jn.distribution[from][to][time] = size;
							}
							break;
						}
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// update junctions circos after reading distribution
		// for (Junction jn : interchange.getJunctions())
		// jn.updateCircos();
	}
}
